﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Shopping.Model;
using Shopping.Common;
using Shopping.DAL;

namespace Shopping.Purview
{
    /// <summary>
    /// 权限校验控制；
    /// </summary>
    public class PrivilegeManage
    {
        private static readonly DALPrivileges dalPrivileges = new DALPrivileges();
        private static readonly DALPurviewResources dalRes = new DALPurviewResources();


        #region "返回指定用户对某资源的可执行操作，并返回为Hash结构 QueryOperatesToHash(int userId,  ResourceEnum resource) by 赵国伟 2011-06-03"
        /// <summary>
        /// 返回指定用户 对某资源的可执行操作，并返回为Hash结构
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="resource"></param>
        /// <returns></returns>
        public static HashSet<string> QueryOperatesToHash(int userId, ResourceEnum resource)
        {
            return dalPrivileges.QueryOperatesToHash(userId, Utility.Cast2int(resource));
        }
        #endregion

        #region 判断指定的用户是否具有某项资源的操作权限；bool IsHavePurview(int userId, ResourceEnum resource, OperateEnum operate) by 赵国伟 2010-04-03
        /// <summary>
        /// 判断指定的用户是否具有某项资源的操作权限；
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="resource"></param>
        /// <param name="operate"></param>
        /// <returns></returns>
        public static bool IsHavePurview(int userId, ResourceEnum resource, OperateEnum operate)
        {
            //获取资源ID
            int resourceId = Utility.Cast2int(resource);
            //获取资源操作ID
            int operateId = Utility.Cast2int(operate);
            if (resourceId == 0 || operateId == 0)
                return false;

            //判断当前资源下是否有指定的操作；
            if (!dalRes.IsHaveOperate(resourceId, operateId))
                return false;
            return dalPrivileges.IsHavePrivilege(userId, resourceId, operateId);
        }
        #endregion

        #region "判断指定的用户拥有指定的资源；IsHavePurview(int userId, ResourceEnum resource) by 赵国伟 2010-10-11"
        /// <summary>
        /// 判断指定的用户拥有指定的资源； by 赵国伟 2010-10-11
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="resource"></param>
        /// <returns></returns>
        public static bool IsHavePurview(int userId, ResourceEnum resource)
        {
            //获取资源ID
            int resourceId = Utility.Cast2int(resource);

            if (resourceId == 0)
                return false;
            return dalPrivileges.IsHavePrivilege(userId, resourceId);

        }
        #endregion

       

        
    }



}
